package com.orhon.smartcampus.modules.student.mutation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.student.dao.ExtendsDao;
import com.orhon.smartcampus.modules.student.entity.Extend;
import com.orhon.smartcampus.modules.student.gqlInputModel.GQLExtendsInput;
import com.orhon.smartcampus.modules.student.inputMapper.ExtendsInputMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddExtendMutation implements DataFetcher {



    @Autowired
    ExtendsDao extendsDao;
    @Autowired
    TokenService tokenService;

    /**
     * 新增/修改届信息
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        GQLExtendsInput input = ExtendsInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
        Extend extend = null;
        
        if (input.getId() != null){
        	extend = extendsDao.get(Extend.class , input.getId());
        	extend.setUpdatedAt(new Date());
        }
        else{
        	extend = new Extend();
            extend.setCreatedAt(new Date());
        }
        
        if(input.getArtdynamic_id() != null){
        	extend.setArtdynamic_id(input.getArtdynamic_id());
        }
        if(input.getStudentId() != null){
        	extend.setStudentId(input.getStudentId());
        }
        if(input.getHome_address() != null){
        	extend.setHome_address(input.getHome_address());
        }
        if(input.getTypecode() != null){
        	extend.setTypecode(input.getTypecode());
        }

        try{
        	extendsDao.saveOrUpdate(extend);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }

        return extend;
    }
}

